//
//  XCNewFeatureController.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/2/29.
//  Copyright © 2016年 小陈. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let ItemCount = 4
class XCNewFeatureController: UICollectionViewController {
   
    
    let FlowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
   
    init(){
        super.init(collectionViewLayout: FlowLayout)
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    override func viewDidLoad() {
        super.viewDidLoad()
     self.collectionView!.registerClass(XCNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //   设置布局
      FlowLayout.itemSize = UIScreen.mainScreen().bounds.size
        FlowLayout.minimumInteritemSpacing = 0
        FlowLayout.minimumLineSpacing = 0
        FlowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }
  
}


// MARK: UICollectionViewDataSource
extension XCNewFeatureController {


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return ItemCount
}
//返回cell
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// 从缓存池中加载cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! XCNewFeatureCell
    /*===================== 在此传递Index.Item 传递出去==============================*/
    /*=====================不能在此设置 TextLable ==============================*/

    cell.itemIndex = indexPath.item

    return cell
  }

    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        // 判断当前显示那一页
        let index = Int(scrollView.contentOffset.x / (collectionView?.bounds.size.width)!)
        // 判断是否是最后一页
        if index == ItemCount-1 {
            //用collectionView 获取cell 执行动画
            let indexPat = NSIndexPath(forRow: index, inSection: 0)
        let cell =   collectionView?.cellForItemAtIndexPath(indexPat) as! XCNewFeatureCell
            cell.scaleStartAnimation()
        }
    }
}




class XCNewFeatureCell: UICollectionViewCell {
    var itemIndex: Int = 0 {
        didSet{
            bgImageView.image = UIImage(named: "new_feature_\(itemIndex + 1)")
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 准备UI
    func prepareUI() {
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        bgImageView.frame.size = UIScreen.mainScreen().bounds.size
        // 开始体验按钮
        // CenterX和父控件CenterX重合
        
        startButton.center.x = UIScreen.mainScreen().bounds.size.width * 0.5
        startButton.center.y = UIScreen.mainScreen().bounds.size.height - 160
     
    }
    func scaleStartAnimation(){
        startButton.hidden = false
       startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(0.5, delay: 0.1, usingSpringWithDamping: 0.4, initialSpringVelocity: 4, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                print("动画执行完成")
        }
    }
    

    
   lazy var bgImageView: UIImageView =  UIImageView(image: UIImage(named: "new_feature_1"))
   
    lazy var startButton:UIButton = {
        let button = UIButton()
        button.showsTouchWhenHighlighted = true
    button.setTitle("开始体验" , forState: UIControlState.Normal)
   button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()

        return button
        }()
    
    
    
    func buttonClick(){
        
        /*=====================显示主界面==============================*/
//let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        // 显示主界面了
        let App =  UIApplication.sharedApplication().delegate as! AppDelegate
        App.window?.rootViewController = XCMainViewController()
        print("开始体验")
    }
}
